using System;
using System.Collections.Generic;
using UnityEngine;

namespace Protobuf
{
    /// <summary>
    /// 账号登录请求协议
    /// </summary>
    [Serializable]
    public class LoginRequest
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public int id;

        /// <summary>
        /// 用户名
        /// </summary>
        public string name;

        /// <summary>
        /// 密码
        /// </summary>
        public string pass;

        /// <summary>
        /// 设备ID
        /// </summary>
        public string deviceId;

        /// <summary>
        /// 平台(iOS/Android/PC)
        /// </summary>
        public string platform;

        public LoginRequest()
        {
            name = "";
            pass = "";
            deviceId = "";
            platform = "";
        }

        public LoginRequest(string name, string pass, string deviceId, string platform)
        {
            this.name = name;
            this.pass = pass;
            this.deviceId = deviceId;
            this.platform = platform;
        }

        /// <summary>
        /// 克隆对象
        /// </summary>
        /// <returns></returns>
        public LoginRequest Clone()
        {
            return new LoginRequest
            {
                id = this.id,
                name = this.name,
                pass = this.pass,
                deviceId = this.deviceId,
                platform = this.platform
            };
        }
    }

    /// <summary>
    /// 登录响应协议
    /// </summary>
    [Serializable]
    public class LoginResponse
    {
        /// <summary>
        /// 返回码 0:成功 其他:失败
        /// </summary>
        public int code;

        /// <summary>
        /// 返回消息
        /// </summary>
        public string message;

        /// <summary>
        /// 玩家信息
        /// </summary>
        public PlayerInfo playerInfo;

        /// <summary>
        /// 登录令牌
        /// </summary>
        public string token;

        public LoginResponse()
        {
            message = "";
            playerInfo = new PlayerInfo();
            token = "";
        }

        /// <summary>
        /// 克隆对象
        /// </summary>
        /// <returns></returns>
        public LoginResponse Clone()
        {
            return new LoginResponse
            {
                code = this.code,
                message = this.message,
                playerInfo = this.playerInfo != null ? this.playerInfo.Clone() : null,
                token = this.token
            };
        }
    }

    /// <summary>
    /// 道具信息
    /// </summary>
    [Serializable]
    public class ItemInfo
    {
        /// <summary>
        /// 道具ID
        /// </summary>
        public int itemId;

        /// <summary>
        /// 数量
        /// </summary>
        public int count;

        public ItemInfo()
        {
        }

        public ItemInfo(int itemId, int count)
        {
            this.itemId = itemId;
            this.count = count;
        }

        /// <summary>
        /// 克隆对象
        /// </summary>
        /// <returns></returns>
        public ItemInfo Clone()
        {
            return new ItemInfo
            {
                itemId = this.itemId,
                count = this.count
            };
        }
    }

    /// <summary>
    /// 英雄信息
    /// </summary>
    [Serializable]
    public class HeroInfo
    {
        /// <summary>
        /// 英雄ID
        /// </summary>
        public int heroId;

        /// <summary>
        /// 英雄等级
        /// </summary>
        public int level;

        /// <summary>
        /// 星级
        /// </summary>
        public int star;

        public HeroInfo()
        {
        }

        public HeroInfo(int heroId, int level, int star)
        {
            this.heroId = heroId;
            this.level = level;
            this.star = star;
        }

        /// <summary>
        /// 克隆对象
        /// </summary>
        /// <returns></returns>
        public HeroInfo Clone()
        {
            return new HeroInfo
            {
                heroId = this.heroId,
                level = this.level,
                star = this.star
            };
        }
    }

    /// <summary>
    /// 玩家信息协议
    /// </summary>
    [Serializable]
    public class PlayerInfo
    {
        /// <summary>
        /// 玩家ID
        /// </summary>
        public long playerId;

        /// <summary>
        /// 昵称
        /// </summary>
        public string nickname;

        /// <summary>
        /// 等级
        /// </summary>
        public int level;

        /// <summary>
        /// 经验值
        /// </summary>
        public long exp;

        /// <summary>
        /// 金币
        /// </summary>
        public long gold;

        /// <summary>
        /// 钻石
        /// </summary>
        public long diamond;

        /// <summary>
        /// 道具列表
        /// </summary>
        public List<ItemInfo> items;

        /// <summary>
        /// 英雄列表
        /// </summary>
        public List<HeroInfo> heroes;

        public PlayerInfo()
        {
            nickname = "";
            items = new List<ItemInfo>();
            heroes = new List<HeroInfo>();
        }

        /// <summary>
        /// 克隆对象
        /// </summary>
        /// <returns></returns>
        public PlayerInfo Clone()
        {
            var clone = new PlayerInfo
            {
                playerId = this.playerId,
                nickname = this.nickname,
                level = this.level,
                exp = this.exp,
                gold = this.gold,
                diamond = this.diamond,
                items = new List<ItemInfo>(),
                heroes = new List<HeroInfo>()
            };

            foreach (var item in this.items)
            {
                clone.items.Add(item.Clone());
            }

            foreach (var hero in this.heroes)
            {
                clone.heroes.Add(hero.Clone());
            }

            return clone;
        }
    }

    /// <summary>
    /// Protobuf序列化工具类
    /// </summary>
    public static class ProtobufSerializer
    {
        /// <summary>
        /// 序列化对象为字节数组
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>字节数组</returns>
        public static byte[] Serialize<T>(T obj)
        {
            string json = JsonUtility.ToJson(obj);
            return System.Text.Encoding.UTF8.GetBytes(json);
        }

        /// <summary>
        /// 反序列化字节数组为对象
        /// </summary>
        /// <param name="data">字节数组</param>
        /// <returns>反序列化的对象</returns>
        public static T Deserialize<T>(byte[] data) where T : new()
        {
            if (data == null || data.Length == 0)
                return new T();

            try
            {
                string json = System.Text.Encoding.UTF8.GetString(data);
                return JsonUtility.FromJson<T>(json);
            }
            catch (Exception ex)
            {
                Debug.LogError("反序列化失败: " + ex.Message);
                return new T();
            }
        }
    }
}